package com.shuoyue.ycgk.ui.pay;

import com.ahammertest.ycgk.wxapi.WxPayBean;
import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.exception.NetErrorException;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import io.reactivex.Observable;
import retrofit2.http.Field;

public interface PreOrderContract {
    interface View extends BaseView {
        void setPriceInfo(OrderPriceInfo priceInfo);

        void getPriceInfoFail();

        void createOrderSuc(OrderNo orderCode, int payType);

        void getWxPayInfoSuc(WxPayBean o);

        void getAliPaySuc(String o);

        void enableButtonPay();
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();


        /**
         * @param productId      商品ID
         * @param productNum     商品数量
         * @param productPriceId 商品价格ID
         * @param productType    商品类型
         * @param payType        支付类型（1：微信；2：支付宝）
         * @param addressId      地址ID
         * @param couponId       优惠券ID
         */
        public void submitOrder(int productId,
                                int productNum,
                                int productPriceId,
                                int productType,
                                int payType,
                                Integer addressId,
                                String couponId,
                                String remark) {

            apply(model.submitOrder(
                    productId,
                    productNum,
                    productPriceId,
                    productType,
                    payType,
                    addressId,
                    couponId,
                    remark))
                    .subscribe(new ApiSubscriber<Optional<OrderNo>>(mView, this) {
                        @Override
                        public void onNext(Optional<OrderNo> listOptional) {
                            super.onNext(listOptional);
                            mView.createOrderSuc(listOptional.getIncludeNull(), payType);
                        }

                        @Override
                        protected void onFail(NetErrorException error) {
                            super.onFail(error);
                            mView.enableButtonPay();
                        }
                    });
        }


        /**
         * @param productId      商品ID
         * @param productNum     商品数量
         * @param productPriceId 商品价格ID
         * @param productType    商品类型
         * @param addressId      地址ID
         * @param couponId       优惠券ID
         */
        public void getPriceInfo(int productId,
                                 int productNum,
                                 int productPriceId,
                                 int productType,
                                 Integer addressId,
                                 String couponId) {
            apply(model.getPriceInfo(
                    productId,
                    productNum,
                    productPriceId,
                    productType,
                    addressId,
                    couponId))
                    .subscribe(new ApiSubscriber<Optional<OrderPriceInfo>>(mView, this) {
                        @Override
                        public void onNext(Optional<OrderPriceInfo> listOptional) {
                            super.onNext(listOptional);
                            mView.setPriceInfo(listOptional.getIncludeNull());
                        }

                        @Override
                        protected void onFail(NetErrorException error) {
                            super.onFail(error);
                            mView.getPriceInfoFail();
                        }
                    });
        }


        public void getWxPayInfo(String orderCode) {
            apply(model.getWxPayInfo(orderCode))
                    .subscribe(new ApiSubscriber<Optional<WxPayBean>>(mView, this) {
                        @Override
                        public void onNext(Optional<WxPayBean> listOptional) {
                            super.onNext(listOptional);
                            mView.getWxPayInfoSuc(listOptional.getIncludeNull());
                            mView.enableButtonPay();
                        }

                        @Override
                        protected void onFail(NetErrorException error) {
                            super.onFail(error);
                            mView.enableButtonPay();
                        }
                    });
        }

        public void getAliPayInfo(String orderCode) {
            apply(model.getAlipayInfo(orderCode))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> listOptional) {
                            super.onNext(listOptional);
                            mView.getAliPaySuc(listOptional.getIncludeNull());
                            mView.enableButtonPay();
                        }

                        @Override
                        protected void onFail(NetErrorException error) {
                            super.onFail(error);
                            mView.enableButtonPay();
                        }
                    });
        }
    }

    class Model extends BaseModel {
        Observable<BaseResult<OrderPriceInfo>> getPriceInfo(int productId,
                                                            int productNum,
                                                            int productPriceId,
                                                            int productType,
                                                            Integer addressId,
                                                            String couponId) {
            return RetrofitClient.getInstance().getApi().getPreOrderPrice(productId,
                    productNum,
                    productPriceId,
                    productType,
                    addressId,
                    couponId);
        }

        Observable<BaseResult<OrderNo>> submitOrder(int productId,
                                                    int productNum,
                                                    int productPriceId,
                                                    int productType,
                                                    int payType,
                                                    Integer addressId,
                                                    String couponId,
                                                    String remark) {
            return RetrofitClient.getInstance().getApi().submitOrder(productId,
                    productNum,
                    productPriceId,
                    productType,
                    payType,
                    addressId,
                    couponId,
                    remark);
        }


        Observable<BaseResult<WxPayBean>> getWxPayInfo(String orderCode) {
            return RetrofitClient.getInstance().getApi().getWxPayInfo(orderCode);
        }

        Observable<BaseResult<String>> getAlipayInfo(String orderCode) {
            return RetrofitClient.getInstance().getApi().getAlipayInfo(orderCode);
        }
    }
}
